// JavaScript Document
var rowid = "row_id";            //tr id
var zoneid = "circle_id";		// zone name td id
var stateid = "state_id";		// disable enable span id
var cancel = "";
function Inint_AJAX() 
{
	try 
	{ 
		return new ActiveXObject("Msxml2.XMLHTTP");  
	} 
	catch(e) 
	{} //IE
	try 
	{ 
		return new ActiveXObject("Microsoft.XMLHTTP"); 
	} 
	catch(e) {} //IE
	try 
	{ 
		return new XMLHttpRequest(); 
	} catch(e) {} //Native Javascript
	alert("XMLHttpRequest not supported");
	return null;
};


function edit_zone(id,value) 
{
	$(".circle_row").hide();
	$("#"+rowid+"_"+id).show();
	cancel = document.getElementById(rowid+"_" + id).innerHTML;
	//alert(cancel);
	var editmode = document.getElementById(rowid+"_" + id);
	editmode.innerHTML = "Loading.....";
	var http = Inint_AJAX();
	http.onreadystatechange = function () 
	{ 	
		if(http.readyState == 4 && http.status == 200) {
			editmode.innerHTML = "";
		editmode.innerHTML = http.responseText;
		}
	};
	http.open("post", "call_ajax_circle_handler.php?edit_id=" + id,true); 
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	http.send(null); 
	

}

function default_row(id){
	$(".circle_row").show();
 document.getElementById(rowid+"_" + id).innerHTML = cancel;	
}

function save_changes(id,circle_name,zone_id){
	var new_name  = document.getElementById(circle_name);
	var new_zoneid  = document.getElementById(zone_id);
	var row_change = document.getElementById(rowid+"_"+id);
	
	if(new_zoneid.value == 0){
		$("#circle_msg").html("zone is required");
		new_zoneid.focus();
		return;
		
	}
	if(new_name.value==""){
	$("#circle_msg").html("circle name is required");
	new_name.focus();
	return;
	}
	

	row_change.innerHTML = "<td colspan='4' align='center'><b>Saving.....</b></td>";
	
	var http = Inint_AJAX();
	http.onreadystatechange = function () 
	{ 	
		if(http.readyState == 4 && http.status == 200) {
		row_change.innerHTML = http.responseText;
		$(".circle_row").show();
		}
	};
	http.open("post", "call_ajax_circle_handler.php?new_circle="+escape(new_name.value)+ "&&new_zone="+ new_zoneid.value +"&&id="+id,true); 
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	http.send(null); 

}


function status_handler(value,id) {
	var changetag = document.getElementById(stateid+"_"+id);
	changetag.innerHTML ="<font style='font-size: 8.5pt' face='Verdana' >Loading....</font>";
	var http = Inint_AJAX();
	http.onreadystatechange = function () 
	{ 	
		if(http.readyState == 4 && http.status == 200) {
		changetag.innerHTML = http.responseText;
		}
	};
	http.open("post", "call_ajax_circle_handler.php?status=" + value + "&&id=" + id,true); 
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	http.send(null); 
	

}

function add_circle(zone,value,msg) {
var circle_name = document.getElementById(value);
var zone_id = document.getElementById(zone);


	if(zone_id.value == 0) {
	$("#"+msg).html("<font color='red'>zone is required</font>");
	zone_id.focus();
	return false;
	}
	else if(circle_name.value ==""){
	$("#"+msg).html("<font color='red'>Circle name is required</font>");
	circle_name.focus();
	return false;
	}
	else {
		$("#circle_msg").html("Saving.....");
		var http = Inint_AJAX();
		http.onreadystatechange = function () 
		{ 	
			if(http.readyState == 4 && http.status == 200) {
			
			$("#table1 > #content_zone:First").prepend(http.responseText);
			$("#circle_msg").html("Circle is added successfully");
			$("#add_form").toggle();
			
			}
		};
		http.open("post", "call_ajax_circle_handler.php?add_circle="+escape(circle_name.value)+ "&&zone_id=" + zone_id.value,true); 
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
		http.send(null); 
		
		circle_name.value ="";
			zone_id.value = 0;


		
	return false;
	}


//	return false;
}

function search_id(value){
	if(value ==""){
	$(".circle_row").show();
	$("#circle_msg").html("");
	return ;
	}
	
	
	
$(".circle_row").hide();

$("#circle_msg").html("Loading....");
var http = Inint_AJAX();
	http.onreadystatechange = function () 
	{ 	
		if(http.readyState == 4 && http.status == 200) {
			//alert(http.responseText);
			
			$("#circle_msg").html("");
		var id_str = JSON.parse(http.responseText);
			if(id_str.length ==0){
			$("#circle_msg").html("No Record Found");
			} 
			for(i=0;i< id_str.length;i++) {
				$("#row_id_"+id_str[i].circle_id).show();
			} 
			
		}
	};
	http.open("post", "call_ajax_circle_handler.php?search=" + value,true); 
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	http.send(null); 
	


}




